package com.xuxk.project.oauth.controller.rpc;

import com.github.pagehelper.Page;
import com.xuxk.project.oauth.annotation.OauthLog;
import com.xuxk.project.oauth.bean.XuxkUser;
import com.xuxk.project.oauth.service.XuxkUserService;
import com.xuxk.project.oauth.util.PageInfoVo;
import com.xuxk.project.oauth.util.PageUtil;
import com.xuxk.project.oauth.util.RequestUtils;
import com.xuxk.project.oauth.vo.XuxkUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 *   @author  橙云科技有限公司
 *  只对内部服务开放，不对第三方开放
 */

@RequestMapping("/rpc")
@RestController
public class XuxkUserController {

    @Autowired
	XuxkUserService xuxkUserService;


    /**
    * 分页查询
    * @param pageNum  页码
    * @param pageSize 页行数
    * @param request
    * 资源标识符： xuxkUser:xuxkUsers
    * @return
    */
    @GetMapping(value = "/v1.0/xuxkUsers", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "分页查询用户信息")
	public PageInfoVo xuxkUsers(Integer pageNum, Integer pageSize, HttpServletRequest request){

        Map<String,Object> paramMap = RequestUtils.getQueryParams(request);

        Page<XuxkUserVo> pageResult = xuxkUserService.listXuxkUsers(paramMap, PageUtil.getPageParam(pageNum,pageSize));

		return new PageInfoVo(pageResult) ;
	}



    @GetMapping(value="/v1.0/xuxkUsers/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "查询用户信息")
    public XuxkUser getXuxkUser(@PathVariable Integer id) {
        XuxkUser xuxkUser = xuxkUserService.getXuxkUser(id );

        return xuxkUser;
    }


    @PostMapping(value="/v1.0/xuxkUsers", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "新增用户信息")
    public Integer saveXuxkUser(@RequestBody XuxkUser xuxkUser) {

        return  xuxkUserService.insertXuxkUser(xuxkUser);

    }

    /**
     * 部分更新
     *
     * @param xuxkUser
     * @return
     */
    @PatchMapping(value="/v1.0/xuxkUsers/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "部分更新用户信息")
    public Integer patchUpdateXuxkUser(@PathVariable Integer id,@RequestBody XuxkUser xuxkUser) {
        xuxkUser.setId(id);

        return   xuxkUserService.patchUpdateXuxkUser(xuxkUser);


    }
    /**
     * 整体更新
     * @param xuxkUser
     * @return
     */
    @PutMapping(value="/v1.0/xuxkUsers/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "整体更新用户信息")
    public Integer updateXuxkUser(@PathVariable Integer id,@RequestBody XuxkUser xuxkUser) {

        xuxkUser.setId(id);

        return  xuxkUserService.updateXuxkUser(xuxkUser);
    }


    @DeleteMapping(value="/v1.0/xuxkUsers/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @OauthLog(eventName = "删除用户信息")
    public Integer deleteXuxkUser(@PathVariable Integer id) {

        xuxkUserService.deleteXuxkUser(id);

        return 1 ;

    }

}
